package com.czl.module_service.viewmodel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.AssetHandleBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class AssetHandleApplyInfoViewModel(application: MyApplication, model: DataRepository) :
    ApprovalFlowViewModel(application, model) {

    val uc = UiChangeEvent()
    val businessId: Int = 11
    var taskUserId: String? = ""
    var bean = ObservableField(AssetHandleBean())
    var checkInfo = ObservableField(false)
    var isCancel = ObservableField(false)
    var dataId: Int? = null
    var userId: String? = ""
    var note = ObservableField("")
    var processStatusObs = ObservableField("")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<AssetHandleBean> = SingleLiveEvent()
    }

    val onBackClickCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        //撤回
        wxApprpveApplicationHandle(4)
    })

    val onAgreeClickCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        //同意
        nextTaskProcess()
    })

    val onRejectClickCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        //驳回
        hangUpTaskProcess()
    })

    private fun nextTaskProcess() {
        model.mTengYunHttpDataSource.nextTaskProcess(
            GsonUtils.toJson(
                mapOf(
                    "taskUserId" to taskUserId,
                    "taskDesc" to note.get()
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        if (t.data == true) {
                            wxApprpveApplicationHandle(2)
                        } else {
                            showNormalToast("审批通过")
                            LiveBusCenter.postAssetResultReturnEvent(1)
                            selectHandleApplicationByHandleApplicationId()
                        }
                    } else {
                        showNormalToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    private fun hangUpTaskProcess() {
        model.mTengYunHttpDataSource.hangUpTaskProcess(
            GsonUtils.toJson(
                mapOf(
                    "taskUserId" to taskUserId,
                    "taskDesc" to note.get()
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        wxApprpveApplicationHandle(3)
                        LiveBusCenter.postAssetResultReturnEvent(1)
                        selectHandleApplicationByHandleApplicationId()
                    } else {
                        showNormalToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    fun wxApprpveApplicationHandle(
        status: Int = 0
    ) {
        if (status < 1) return
        model.mTengYunHttpDataSource.wxApprpveApplicationHandle(
            GsonUtils.toJson(
                mapOf(
                    "handleApplicationId" to dataId,
                    "userId" to model.getLocalUserInfo()?.userId,
                    "companyId" to model.getLoginCompanyId()?.toInt(),
                    "status" to status
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        if (status == 2) {
                            showNormalToast("审批通过")
                        }
                        if (status == 3) {
                            showNormalToast("驳回成功")
                        }
                        if (status == 4) {
                            showNormalToast("撤回成功")
                        }
                        LiveBusCenter.postAssetResultReturnEvent(1)
                        selectHandleApplicationByHandleApplicationId()
                    } else {
                        showNormalToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    fun setData(data: AssetHandleBean?) {
        data?.let {
            bean.set(data)
        }
    }

    fun initCheckInfo(
        processStatus: Int? = 3,
        taskStatus: Int? = 0
    ) {
        if (processStatus == 0) {
            processStatusObs.set("已驳回")
        } else if (processStatus == 1) {
            processStatusObs.set("待审核")
        } else if (processStatus == 2) {
            processStatusObs.set("审批通过")
        } else if (processStatus == 3) {
            processStatusObs.set("已撤回")
        }
        checkInfo.set(taskStatus == 2)
        if (taskStatus == 0) {
            isCanCancel()
        }
    }

    fun getDataInfo(
        handleApplicationId: Int? = null
    ) {
        if (handleApplicationId == null || handleApplicationId < 1) return
        userId = model.getLocalUserInfo()?.userId
        dataId = handleApplicationId
        selectHandleApplicationByHandleApplicationId()
    }

    fun isCanCancel() {
        model.mTengYunHttpDataSource.isCanCancel(
            GsonUtils.toJson(
                mapOf(
                    "userId" to userId,
                    "relationId" to dataId,
                    "businessId" to businessId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        isCancel.set(t.data as Boolean)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun selectHandleApplicationByHandleApplicationId() {
        model.mTengYunHttpDataSource.selectHandleApplicationByHandleApplicationId(
            GsonUtils.toJson(
                mapOf(
                    "handleApplicationId" to dataId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<AssetHandleBean>>() {
                override fun onResult(t: BaseBean<AssetHandleBean>) {
                    if (t.state == 0) {
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

}